package com.ssm.resource.server.controller.video;


import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.resource.dao.model.Video;
import com.ssm.resource.rpc.api.video.VideoService;
import com.ssm.upms.log.AdminControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 视频资源管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/video")
public class VideoController  {


    /**
     * 进入视频资源添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("video:add")
    public String add(ModelMap model){

        model.put("longId", UUIDFactory.getStringId());
        return "manager/video/add";
    }


    /**
     * 进入视频资源编辑页面
     * @param model 返回视频资源的容器
     * @param id 视频资源id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("video:edit")
    public String edit(ModelMap model, @RequestParam String id) {

        model.put("data",videoService.selectById(id));
        return "manager/video/edit";
    }


    /**
     * 视频资源添加
     * @param video 带id的视频资源对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("video:add")
    @AdminControllerLog(description = "添加视频资源" )
    public Tip save(Video video)  {

        try {
            videoService.insert(video);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改视频资源
     * @param video 带id的视频资源对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("video:edit")
    @AdminControllerLog(description = "修改视频资源" )
    public Tip update(Video video) {

        try {
            videoService.updateById(video);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除视频资源
     * @param id 视频资源id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("video:delete")
    @AdminControllerLog(description = "删除视频资源" )
    public Tip delete(@RequestParam String id) {

        try {
            videoService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 视频资源id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("video:delete")
    @AdminControllerLog(description = "批量删除视频资源" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            videoService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找视频资源
     * @param id 视频资源id
     * @return 视频资源对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("video:list")
    public Video find(@RequestParam String id) {

        return videoService.selectById(id);
    }


    /**
     * 获取视频资源列表 获取全部 不分页
     * @param request 请求参数
     * @return 视频资源列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("video:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取视频资源列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 视频资源列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("video:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {

        modelMap.put("pageInfo",videoService.list(new ParamMap(request),page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/video/list";
    }


    //---------------------------- property -------------------------------

    @Resource
    private VideoService videoService;

}
